import { _decorator, Collider2D, Color, Component, Contact2DType, EventTouch, math, Node, PhysicsSystem2D, Vec2, Vec3 } from 'cc';
import { UnitPlanetBase } from './UnitPlanetBase';
import { UnitEvent } from './Unit';
import { Global, UnitType } from '../Util/Global';
import { UICtrl } from '../Ctrl/UICtrl';
import { GameStickerType } from '../Model/GameStickerMgr';
import AssetsMgr from '../Model/AssetsMgr';
import { Bullet_3 } from './Bullet_3';
import SoundCtrl from '../Ctrl/SoundCtrl';
const { ccclass, property } = _decorator;

@ccclass('UnitSun')
export class UnitSun extends UnitPlanetBase {
    get helium_countdown() {
        return this._helium_countdown;
    }
    set helium_countdown(val) {
        this._helium_countdown = val;
    }
    private _helium_countdown: number = 0;

    protected start(): void {
        super.start();

        this.unitType = UnitType.UnitSun;
    }

    /**替代lateUpdate，不会影响下基类的lateUpdate逻辑 */
    protected lateUpdateLogic(dt: number) {

        //如果有环绕对象进行绑定环绕
        if (this._forceTrackTarget && this.FixedTrackMove(dt)) {
            this.UpdateBiggerTargetPlanetForce(UnitType.UnitPlanet);
        }
        else {
            this.UpdateOtherPlanetForce();
        }

        this.rigBody.linearVelocity = this.rigBody.linearVelocity.add(this._RBVFrame_GravityAdd);
        this.rigBody.linearVelocity = this.rigBody.linearVelocity.add(this._RBVFrame_MoveAdd);
        this.Stand(dt, 0.001, this.minStandSpeed);
        this._RBVFrame_MoveAdd.set(0, 0);
        this._RBVFrame_GravityAdd.set(0, 0);

        this.OnHelium(dt);
    }

    onCheckRocheLimitShow(planet?: UnitPlanetBase, add: boolean = true) {
        if (!planet) {
            planet = this.FilterSelfImpactPlanet_One((planet) => {
                return planet.playerCtrl;
            });
            if (planet == null) {
                UICtrl.ClearStickerRenderer(this, GameStickerType.RocheLimitTipSticker);
            }
        }
        if (planet && planet.playerCtrl) {
            if (add && !planet.forceTrackTarget && !this.forceTrackTarget) {
                UICtrl.CreateStickerRenderer(this, GameStickerType.RocheLimitTipSticker);
            }
            else {
                UICtrl.ClearStickerRenderer(this, GameStickerType.RocheLimitTipSticker);
            }
        }
    }

    protected onTriggerEnter(selfCollider: Collider2D, otherCollider: Collider2D) {
        if (!this.isValid) {
            return;
        }

        let other = otherCollider.node.getComponent(UnitPlanetBase);

        if (other) {
            this.CheckPlanetOnTrank(other);
            other.AddPlanetGravityImpact(this);
            this.onCheckRocheLimitShow(other, true);
        }
    }

    protected onTriggerExit(selfCollider: Collider2D, otherCollider: Collider2D) {
        if (!this.isValid) {
            return;
        }

        let other = otherCollider.node.getComponent(UnitPlanetBase);

        //移除自身引力影响
        if (other) {
            other.RemovePlanetGravityImpact(this);
            this.onCheckRocheLimitShow(other, false);
        }
    }

    OnHelium(dt: number) {
        let boom = false;
        if (dt > 0) {
            //氦闪
            let curT = this._helium_countdown - dt;
            if (this._helium_countdown > 0 && curT < 0) {//这一帧爆发
                boom = true;
            }
            // if (curT > -1 && curT < 0) {//持续1秒

            // }
            this._helium_countdown = Math.max(-1, curT);
        }
        else {
            boom = true;
            this._helium_countdown = -1;
        }

        if (boom) {

            let wpos = this.unitWorldPos;
            let spawnFinish = (node) => {
                let bullet = node.getComponent(Bullet_3);
                if (bullet) {
                    bullet.maxRadius = this.alertFieldRadius;
                    bullet.canLifeTime = 1;
                    bullet.atk = 999;
                    bullet.parentUnit = this;

                    SoundCtrl.playEffect('boom', 0.5);
                    Global.gameManager.mapCtrl.mapSpawner
                        .SpwanPrefabImmediate(wpos, AssetsMgr.plistPrefabMap.get('Sun_Boom_Plist'));

                }
            }
            Global.gameManager.mapCtrl.mapSpawner
                .SpawnUnitImmediately(
                    AssetsMgr.getUnitPrefab(UnitType.Bullet, 'Bullet3'),
                    wpos,
                    1,
                    spawnFinish
                );
        }

    }

    protected onColliderEnter(selfCollider: Collider2D, otherCollider: Collider2D) {
        if (!this.isValid) {
            return;
        }
        let other = otherCollider.node.getComponent(UnitPlanetBase);

        if (otherCollider.sensor) {
        }
        else {
            if (other.unitType > this.unitType) {
                // this.node.destroy();//还不能碰撞立即销毁

                if (!this.already_initialized) {//碰撞事件比update先，有可能碰撞发生时，目标_destroy还没重置
                    //给个速度让其离开碰撞区域
                    let dir = this.unitWorldPos.subtract(other.unitWorldPos);
                    this.MoveTo_byDir(dir, true, 500);//给个反向速度
                }
                this.OnHelium(-1);
                this.DestroySelf("被大质量星体撞毁！");

                let dir = other.unitWorldPos.subtract(this.unitWorldPos);
                other.MoveTo_byDir(dir, false, 10 * this.mass / other.mass);//给个反向速度

            }
            else if (other.unitType == this.unitType) {
                let remain = this.mass - other.mass;
                if (remain <= 0) {
                    if (!this.already_initialized) {//碰撞事件比update先，有可能碰撞发生时，目标_destroy还没重置
                        //给个速度让其离开碰撞区域
                        let dir = this.unitWorldPos.subtract(other.unitWorldPos);
                        this.MoveTo_byDir(dir, true, 500);//给个反向速度
                    }
                    this.OnHelium(-1);
                    this.DestroySelf("被星体撞击丢失维持自身的最低质量！");
                }
                else {
                    let dir = this.unitWorldPos.subtract(other.unitWorldPos);
                    this.MoveTo_byDir(dir, true, 500);//给个反向速度
                    this.mass = remain;
                }
            }
        }

    }
}


